package cn.brilliantfuture.commentry.alarm.impl;

import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.wsf.spi.annotation.WebContext;

import cn.brilliantfuture.commentry.alarm.AlarmMessageRemote;
import cn.brilliantfuture.commentry.alarm.AlarmMessageWebService;
import cn.brilliantfuture.commentry.entity.AlarmInfo;
import cn.brilliantfuture.commentry.entity.Lockers;
import cn.brilliantfuture.commentry.entity.StorageGrid;
import cn.brilliantfuture.commentry.util.SMSScheduler;

/**
 * 
 * Title: Communication Entry
 * 
 * Author: LiTian
 * 
 * Date: 2011-11-2
 * 
 * Description: 告警信息类
 * 
 */
@Stateless
@WebService(endpointInterface = "cn.brilliantfuture.commentry.alarm.AlarmMessageWebService")
@WebContext(contextRoot = "/commentry", urlPattern = "/AlarmMessageWebService")
public class AlarmMessageImpl implements AlarmMessageRemote, AlarmMessageWebService {

	@PersistenceContext(unitName = "commentry-jpa")
	private EntityManager entityManager;

	@SuppressWarnings("unchecked")
	public void getAlarmData(String deviceSn, Integer gridNo, String rfidSn, Integer alarmType, Float alarmValue) {

		List<Lockers> lockersList = entityManager.createQuery("select l from Lockers l where l.deleted = 0 and l.device.deviceSn = '" + deviceSn + "'").getResultList();
		
		if (null != lockersList && lockersList.size() > 0){
			Lockers lockers = lockersList.get(0);
			
			StringBuffer description = new StringBuffer();
			description.append(lockers.getLocation().getDescription() + lockers.getLockersName());
			
			AlarmInfo alarmInfo = new AlarmInfo();
			alarmInfo.setAlarmType(alarmType);
			alarmInfo.setLockersId(lockers.getLockersId());
			alarmInfo.setInsertTime(new Date());
			
			if (alarmType == 1 || alarmType == 4 || alarmType == 6 || alarmType == 7 || alarmType == 8 || alarmType == 9){
				if (alarmType == 1){
					lockers.setTempBoardState(false);
				} else if (alarmType == 4){
					lockers.setLockBoardState(false);
				} else if (alarmType == 6){
					lockers.setKeyboardState(false);
				} else if (alarmType == 7){
					lockers.setReaderState(false);
				} else if (alarmType == 8){
					lockers.setBarcodeGunState(false);
				} else if (alarmType == 9){
					lockers.setLedBoardState(false);
				}
				
				entityManager.merge(lockers);
			}
			
			
			if (alarmType == 3 || alarmType == 2 || alarmType == 5 || alarmType == 10){
				List<StorageGrid> storageGridList = entityManager.createQuery("select sg from StorageGrid sg where sg.deleted = 0 and sg.lockers.lockersId = " + lockers.getLockersId() + " and sg.gridNum = " + gridNo).getResultList();
				StorageGrid storageGrid = storageGridList.get(0);
				
				if (alarmType == 2){
					storageGrid.setTempWorkState(false);
				}  else if (alarmType == 5){
					storageGrid.setLockWorkState(false);
				} else if (alarmType == 10){
					storageGrid.setOpend(true);
				}
				
				description.append(storageGrid.getDescription());
				
				if (alarmType != 3)
					entityManager.merge(storageGrid);
			} 
			
			description.append(getAlarmMessage(alarmType));
			
			if (alarmType == 3 && null != rfidSn && null != alarmValue){
				description.append(",货码为").append(rfidSn).append(",告警数据值为").append(alarmValue.floatValue());
			}
			
			alarmInfo.setDescription(description.toString());
			
			entityManager.persist(alarmInfo);
			entityManager.flush();
			
			/*List<Object> objs = entityManager.createQuery("select ap.phone from AlarmPerson ap where ap.deleted = 0").getResultList();
			StringBuffer phones = new StringBuffer();
			if (null != objs && objs.size() > 0) {
				for (int i = 0; i < objs.size(); i++) {
					phones.append((String) objs.get(i));
					if (objs.size() - 1 != i)
						phones.append(",");
				}
				new SMSScheduler(phones.toString(), description.toString(), 2, null).start();
			}*/
		}
	}
	
	private String getAlarmMessage(int alarmType){
		switch (alarmType) {
		case 1:
			return "温控设备无法启动告警";
		case 2:
			return "温控执行设备工作异常(不能加热或制冷)";
		case 3:
			return "温度告警";
		case 4:
			return "门锁控制设备工作异常告警";
		case 5:
			return "门锁工作异常告警";
		case 6:
			return "键盘工作异常告警";
		case 7:
			return "读卡器工作异常告警";
		case 8:
			return "扫描枪工作异常告警";
		case 9:
			return "LED工作异常告警";
		case 10:
			return "未关闭告警";
		default:
			return "其它告警";
		}
	}
}
